package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    //向注册中心订阅接口服务
    @Reference
    private CheckItemService checkItemService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //传递给业务层处理
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        //返回分页查询结果给浏览器
        return pageResult;
    }

    /**
     * 新增
     *
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            //将接收的对象传递给业务层处理
            checkItemService.save(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //失败返回
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        //成功返回
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/delete")
    public Result deleteById(Integer id) {
        try {
            //将接收的对象传递给业务层处理
            checkItemService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //失败返回
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        //成功返回
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            //将接收的对象传递给业务层处理
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //失败返回
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 更新
     *
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            //将接收的对象传递给业务层处理
            checkItemService.update(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //失败返回
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        //成功返回
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询检查组（不带分页）
     *
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);//查询失败
        }
    }
}

